 var questans=new Array();
 var assignmentarray=new Array();
 var ansarray=new Array();
 var multipleansarray=new Array();


 	 var mainpath = location.href;
				 
	          if( mainpath.indexOf( "index.php" ) !== -1 )
	            {
			  mainpath=mainpath.substr(0, strrpos(mainpath,"/index.php")); 
	             }



 
function loadchapters(courseid,divid)
{
	if(courseid=="-1")
	{
	 $("#"+divid).html("");
	 $('#pager').hide();
	}

	else{
	var dataString ="courseid="+courseid;
     
          
          $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/loadsyllabus",
		  data:dataString,

     success: function(result){
		if($.trim(result) != "No Result found")
		   
		  $("#"+divid).html(result);
      
	      }
	 
	 }); }
}
 
  function loadchapterschange(courseid,divid)
     {
 if(courseid=="-1")
	{
	 $("#"+divid).html("");
	 $('#pager').hide();
	}

	else{
	var dataString ="courseid="+courseid;
     
          
          $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/loadsyllabuschange",
		  data:dataString,

     success: function(result){
		if($.trim(result) != "No Result found")
		   
		  $("#"+divid).html(result);
      
	      }
	 
	 }); }
}





function loadassignmentchangeandstudent(courseid,chapterdivid,studentdivid)

{
 if(courseid=="-1")
	{
	 $("#"+chapterdivid).html("");
	 $("#"+studentdivid).html("");
	 $('#pager').hide();

	}

	else{
	var dataString ="courseid="+courseid;
     
          
          $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/loadsyllabuschangeandstudents",
		  data:dataString,

     success: function(result){

var resultarray=result.split("||");
 
 $("#"+chapterdivid).html(resultarray[0]);
 $("#"+studentdivid).html(resultarray[1]);

		 
      
	      }
	 
	 }); }

}


function assignassignment(studentid){
   var assignments=assignmentarray;
 
 if(assignments.length==0)
	{
	 $("#questionError").show();
	}
	else{

	 
var dataString ="studentid="+studentid+"&assignments="+assignments;
     
          
          $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/assignassignmenttostudents",
		  data:dataString,

     success: function(result){
		 
		   
		  alert(result);
		  window.location.href = self.location.href;
      
	      }
	 
	 });}
}




function loadcoursequestion()
{
	 
courseid=$("#course_combov").val();
chapterno=$("#chapternamesv").val();
multiplechoice=$("#option_multi").val();
 if(courseid=="-1" )
	{
	 $("#chapter_names").html("");
	 $('#pager').hide();
	}
else if(chapterno=="-1")
	{
	$("#questions").html("");
	$('#pager').hide();
	}

else if(multiplechoice=="-1")
	{
 $("#option_multierror").show();
	 
	}


	else{

 $(".errors").hide();
		
 var dataString ="courseid="+courseid+"&chapterno="+chapterno+"&multiplechoice="+multiplechoice;
 
 
         $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/loadcoursequestionandstudent",
		  data:dataString,

     success: function(result){
      		 $('#questions').html(result);
             $('#pager').show();
			 runpagger();
		  }
	 
	 }); }
	 
	 }	
         

 
function loadassignments()
{
  $('#pager').hide();
courseid=$("#asscourse_combo").val();
chapterno=$("#asschapternames").val();
multiplechoice=$("#option_multi").val();

 if(courseid=="-1" )
	{
	 $("#chapter_names").html("");
	 $('#pager').hide();
	}
else if(chapterno=="-1")
	{
	$("#assigments").html("");
	$('#pager').hide();
	
	}
else if(multiplechoice=="-1")
	{
 $("#option_multierror").show();
	 
	}



	else{
 var dataString ="courseid="+courseid+"&chapterno="+chapterno+"&multiplechoice="+multiplechoice;
 
 
         $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/loadassignments",
		  data:dataString,

     success: function(result){
      		 $('#assigments').html(result);
			 $('#pager').show();
			 runpagger();
		  }
	 
	 }); }
	 
	 }	
        function runpagger(){
        $("#insured_list")
		.tablesorter({widthFixed: true, widgets: ['zebra']})
		.tablesorterPager({container: $("#pager")}); 
		}


function submitassenments(courseid,chapterid,assgname,assgdesc,optionmulti){ 

var dataString ="courseid="+courseid+"&chapterid="+chapterid+"&assgname="+assgname+"&assgdesc="+assgdesc+"&questionarray="+questans+"&optionmultiple="+optionmulti;
     
          
          $.ajax({
          type:'POST',
	 	  url: "../loginsuccess/insertassignment",
		  data:dataString,

     success: function(result){
		 
		   
		  alert(result);
		  window.location.href = self.location.href;
      
	      }
	 
	 });
          
	 
	 }
function submitpopupdetails(email){

	 var dataString ="email="+email;
	 var mainpath = location.href;
	          if( mainpath.indexOf( "index.php" ) !== -1 )
	           {
			  mainpath=mainpath.substr(0, strrpos(mainpath,"/index.php")); 
	             } 

				  $.ajax({
				  type:'POST',
				  url: mainpath+"index.php/login/savepopupemail",
				  data:dataString,

           success: function(result){
			   
			   closeLightbox();
     		//$("#contact").hide(); 
			//$("#mask").fadeIn(0.7);
		  }
	 
	        }); 
			
			}

			function strrpos (haystack, needle, offset) {
    
    var i = -1;
    if (offset) {
        i = (haystack + '').slice(offset).lastIndexOf(needle); // strrpos' offset indicates starting point of range till end,        // while lastIndexOf's optional 2nd argument indicates ending point of range from the beginning
        if (i !== -1) {
            i += offset;
        }
    } else {        i = (haystack + '').lastIndexOf(needle);
    }
    return i >= 0 ? i : false;
}




 var questans=new Array();
 var counter=0;
 function updateQuestionsStrings(questionno,hidedivid,showdivid,actionfunction,index)
   {
	  
	  if(actionfunction=="addquestion")  
	   {
      questans[counter]=questionno;
	  counter++;
	   }

	   else if(actionfunction=="removequestion")  
	   {
		   for(var i=0; i<questans.length; i++) {
            if(questans[i] == questionno) {
            questans.splice(i, 1);
             
        }
    }
        
	   }
	  
$("#"+hidedivid).hide();
$("#"+showdivid).show();

   }



var counternumber=0;
 function updateAssignmentStrings(assgnno,hidedivid,showdivid,actionfunction)
   {
	  
	  if(actionfunction=="addassignment")  
	   {
      assignmentarray[counternumber]=assgnno;
	  counternumber++;
	   }

	   else if(actionfunction=="removeassignment")  
	   {
		   for(var i=0; i<assignmentarray.length; i++) {
            if(assignmentarray[i] == assgnno) {
            assignmentarray.splice(i, 1);
             
        }
    }
        
	   }
	  
$("#"+hidedivid).hide();
$("#"+showdivid).show();

   }
 var indexquestno=0;
 var radiooldquestno="";


function radioSubmit(option,questionno)
   {
   var isreplace=1;
   var tosave=questionno+"-"+option;
	for(var i=0; i < ansarray.length; i++) {
		     var quesarray=ansarray[i].split("-");
  			 if(questionno == quesarray[0])
		      {
				 ansarray.splice(i,1,tosave);
				 isreplace=0;
                
              }
			   
	    }
 if(isreplace==1)
	   {
      ansarray[indexquestno]=tosave;
	  indexquestno++;
	  
	   }
 
	
	 
	
}




var position=0;
var oldquestno="";
function checkSubmit(option,questionno)
   {
	   if(ansarray.length==0)
	   {
        ansarray[position]=questionno+"-"+option;
        position++;
	   }
	   else{
	   ans="";
	     for(var i=0; i < ansarray.length; i++) {


		     var quesarray=ansarray[i].split("-");
                 var optionexist=true;
  			 if(questionno == quesarray[0])
		         {
				 var isexist=false;
				
				 var qq=quesarray[1].split("**");
				 // qq=quesarray[1];

				 
               var abc=new Array();
                   var temp=0;
               for(var j=0; j < qq.length; j++) {
				     
				 if(option==qq[j])
					 {
					 temp=j;
					 for(var k=0; k < qq.length; k++) {
                    abc[k]=qq[k];
				
                    isexist=true;

			  }
			  
			  }}

              if(isexist){
				      abc.splice(temp,1);
					  var replacestr="";
                   for(var m=0; m < abc.length; m++) {
					   if(m==0)
					   {
                    replacestr=abc[m];
					   }
					   else{
                  replacestr=replacestr+"**"+abc[m];
					   }
						  
				}


                       var tosave=questionno+"-"+replacestr;
					  
					   ansarray.splice(i,1,tosave);
                       
					    optionexist=false;
			  }
					  
			   

			   if(optionexist){
				 /*this update the options*/
					 
				 ans=quesarray[1]+"**"+option;
				 var tosave=questionno+"-"+ans;
	             ansarray.splice(i,1,tosave);
				 }
			   
			    }
				else
			     { 
					 
					 ans=option;
					 var tosave=questionno+"-"+ans;
	                 ansarray[position]=tosave;
					 position++;
			     }
			     }
 
	         }
			  

 
  
}
 
function submitattemptedassignment(assignmentrowid,userid)
{
	
var dataString ="userid="+userid+"&assignmentrowid="+assignmentrowid+"&answers="+ansarray;
 
	 var mainpath = location.href;
				 
	          if( mainpath.indexOf( "index.php" ) !== -1 )
	           {
			  mainpath=mainpath.substr(0, strrpos(mainpath,"/index.php")); 
	             } 

				 $.ajax({
          type:'POST',
	 	  url: mainpath+"/index.php/loginsuccess/submitattemptedassignment",
		  data:dataString,

           success: function(result){
     		  alert(result);
			  window.location.href = mainpath+"/index.php/loginsuccess/userassignment";

		  }
	 
	        }); 
}

function closeLightbox() {
                var s = $self[0].style;
                if (opts.destroyOnClose) {
                    $self.add($overlay).remove();
                } else {
                    $self.add($overlay).hide();
                }

                //show the hidden parent lightbox
                if (opts.parentLightbox) {
                    opts.parentLightbox.fadeIn(200);
                }

                $iframe.remove();
                
				// clean up events.
                $self.undelegate(opts.closeSelector, "click");

                $(window).unbind('resize', setOverlayHeight);
                $(window).unbind('resize', setSelfPosition);
                $(window).unbind('scroll', setSelfPosition);
                $(window).unbind('keypress', observeKeyPress);
                if (ie6)
                    s.removeExpression('top');
                opts.onClose();
            }

			 


function applypagger(){
        $("#assignment_result")
		.tablesorter({widthFixed: true, widgets: ['zebra']})
		.tablesorterPager({container: $("#pager")}); 
		}


 